#import "ExpenseEditController.h"
#import "DatePickerController.h"

@implementation ExpenseEditController

@synthesize name, type, exp, amount, date, note;

- (IBAction)datePressed
{
	DatePickerController* ec = [[DatePickerController alloc] initWithNibName:@"DatePickerController" bundle:[NSBundle mainBundle]];
	[ec setDate:exp]; //set current date
	//[self.navigationController setNavigationBarHidden:NO animated:YES];
	[[self navigationController] pushViewController:ec animated:YES];
	[ec release];
	
}

- (IBAction)typePressed
{
	
}

- (IBAction)expensePressed
{
	

}

-(void) viewDidLoad
{
	//self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	[self.view setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"gradientBackground.png"]]];
	//self.navigationItem.leftBarButtonItem.title = @"Back";	
}


- (void)viewWillAppear:(BOOL)animated {
	
    [super viewWillAppear:animated];
	
	//[self.navigationController setNavigationBarHidden:YES animated:YES];
	
	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]
					  init] autorelease];
	
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	
	date.text = [dateFormatter stringFromDate: [exp date]];
}

-(void) setValues:(Expense*) expense
{
	exp = expense;

	name.text = exp.ename;
	type.text = exp.type;	
	amount.text = [NSString stringWithFormat:@"%.2f", exp.amount];
	note.text = exp.note;
	
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	/*
	if(textField == amount)
	{
		CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -80.0);	
		[self.view setTransform:myTransform];
	}
	
	else if(textField == type)
	{
		CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -10.0);	
		[self.view setTransform:myTransform];
	}*/
	
	return YES;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
	//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -200.0);	
	//[self.view setTransform:myTransform];	
	return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
	//set fields if changed
	if(textField == name && [textField.text length ] > 0)
	{
		exp.ename = textField.text;
	}
	else if(textField == type && [textField.text length ] > 0)
	{
		exp.type = textField.text;
	}
	else if(textField == amount && [textField.text length ] > 0)
	{
		exp.amount = [textField.text floatValue];
	}
	
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
	if([text compare:@"\n"] == 0) //check for return entered
	{
		//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 0.0);	
		//[self.view setTransform:myTransform];	
		[textView resignFirstResponder]; //close keyboard on screen
	}
	
	return YES;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{	
	if([string compare:@"\n"] == 0) //check for return entered
	{
		[textField resignFirstResponder]; //close keyboard on screen
		
		//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 0.0);	
		//[self.view setTransform:myTransform];
	}
	
	return YES;
}


-(void) dealloc
{
	[amount release];
	[name release];
	[type release];
	[note release];
	[super dealloc];
}


@end
